<?php
//课时
defined('IN_TS') or die('Access Denied.');

switch($ts){
	case "":
	
		$lessonid = intval($_GET['id']);		
		$strLesson = $new['study']->find('study_lesson',array(
			'lessonid'=>$lessonid,
		));
		
		$strLesson['title'] = stripslashes($strLesson['title']);
		$strLesson['content'] = tsDecode($strLesson['content']);
		
		$strLesson['user'] = aac('user')->getOneUser($strLesson['userid']); 

		
		/*统一控制课程头部信息开始*****************/
		$strStudy = $new['study']->getOneStudy($strLesson['studyid']);
		
		if($strStudy['studyid'] == ''){
			header ( "HTTP/1.1 404 Not Found" );
			header ( "Status: 404 Not Found" );
			$title = '404';
			include pubTemplate ( "404" );
			exit ();
		}
		
		$strStudy['user'] = aac('user')->getOneUser($strStudy['userid']);
		//获取课程的分类
		$strStudy['onecate'] = $new['study']->find('study_cates',array(
			'cateid'=>$strStudy['cateid'],
		));
		
		//$strStudy['twocate'] = $new['study']->find('study_cates',array(
			//'cateid2'=>$strStudy['cateid2'],
		//));
		
		
		
		
		//判断用户学习状态,未学习为空，学习了获取学习信息
		$strStudy['learn'] = '';
		if(intval($TS_USER['userid'])){
			//用户学习状态
			$strStudy['learn'] = $new['study']->find('study_learn',array(
				'userid'=>intval($TS_USER['userid']),
				'studyid'=>$strStudy['studyid'],
			));
			//用户已经学课时
			$arrLessonUsers = $new['study']->findAll('study_lesson_user',array(
				'userid'=>intval($TS_USER['userid']),
				'studyid'=>$studyid,
			));
		}
		
		
		
		/*统一控制课程头部信息结束******************/
		
		
		
		
		/////////////////分类导航开始/////////////////
		//一级分类信息
		$oneCate = $new['study']->find('study_cates',array(
			'cateid'=>$strStudy['cateid'],
		));
		//二级分类列表
		$arrTwoCate = $new['study']->findAll('study_cates',array(
			'recateid'=>$oneCate['cateid'],
		));
		/////////////////分类导航结束/////////////////
		

		
		$isLessonUser = 0;//是否学习完此课时的人
		
		if($TS_USER['userid']){
		
			$userid = $TS_USER['userid'];
			
			
			//是否学习完此课程
			if($strLesson['userid'] != $userid){
				$isLessonUser = $new['study']->findCount('study_lesson_user',array(
					'userid'=>$userid,
					'lessonid'=>$lessonid,
				));
			}
		
		}



        //课时下的视频
        if($strLesson['videoid']){
            $strVideo = $new['study']->find('study_video',array(
                'videoid'=>$strLesson['videoid'],
            ));
        }
		
				
		//相关课程
		$arrRelatedStudys = $new['study']->findAll('study',array(
			'cateid'=>$strStudy['cateid'],
			'isaudit'=>0,
		),'addtime desc',null,3);

		foreach($arrRelatedStudys as $key=>$item){
			$arrRelatedStudy[] = $item;
			$arrRelatedStudy[$key]['user'] = aac('user')->getOneUser($item['userid']);
		}
		
		//课程下课时列表
		$arrLesson = $new['study']->findAll('study_lesson',array(
			'studyid'=>$strStudy['studyid'],
		),'orderid asc');
		
		//最近学完课时的人 
		$arrLessonUsers = $new['study']->findAll('study_lesson_user',array(
			'lessonid'=>$strLesson['lessonid'],
		),'addtime desc',null,40);
		foreach($arrLessonUsers as $key=>$item){
		
			$arrLessonUser[] = aac('user')->getOneUser($item['userid']);
		
		}
		
		//课程评论列表
		$page = isset ( $_GET ['page'] ) ? intval ( $_GET ['page'] ) : 1;
		$url = tsUrl ( 'study', 'lesson', array ('id'=>$strLesson['lessonid'],'page' =>'') );
		$lstart = $page * 15 - 15;
		$arrComments = $new['study']->findAll('study_lesson_comment',array(
			'lessonid'=>$strLesson['lessonid'],
		),'addtime desc',null,$lstart.',15');
		
		$commentNum = $new ['study']->findCount ( 'study_lesson_comment', array (
			'lessonid'=>$strLesson['lessonid'],
		) );
		$pageUrl = pagination ( $commentNum, 15, $page, $url );
		
		foreach($arrComments as $key=>$item){
			$arrComment[] = $item;
			$arrComment[$key]['user'] = aac('user')->getOneUser($item['userid']);
		}

		$title = '第'.$strLesson['orderid'].'课 '.$strLesson['title'].'_'.$strStudy['studyname'];
		include template('lesson');
	
		break;
		
		
	//学过此课时
	case "do":
		
		$userid = intval($TS_USER['userid']);
		
		if($userid == 0){
			echo 0;exit;
		}
		
		$lessonid = intval($_POST['lessonid']);
		$studyid = intval($_POST['studyid']);
		
		$isUser = $new['study']->findCount('study_lesson_user',array(
			'userid'=>$userid,
			'lessonid'=>$lessonid,
		));
		
		if($isUser > 0){
			echo 1;exit;
		}
		
		$new['study']->create('study_lesson_user',array(
			'userid'=>$userid,
			'lessonid'=>$lessonid,
			'studyid'=>$studyid,
			'addtime'=>time(),
		));
		
		//同时判断加统计
		$isLearn =$new['study']->findCount('study_learn',array(
			'userid'=>$userid,
			'studyid'=>$studyid,
		));
		
		if($isLearn == 0){
		
			$new['study']->create('study_learn',array(
				'userid'=>$userid,
				'studyid'=>$studyid,
				'count_lesson'=>1,
				'status'=>1,
				'addtime'=>time(),
			));
			
			//统计更新学习人数
			$count_learn= $new['study']->findCount('study_learn',array(
				'studyid'=>$studyid,
			));
			
			$new['study']->update('study',array(
				'studyid'=>$studyid,
			),array(
				'count_learn'=>$count_learn,
			));
		
		}else{
		
			//统计学了多少个课时
			$count_lesson = $new['study']->findCount('study_lesson_user',array(
				'userid'=>$userid,
				'studyid'=>$studyid,
			));
			
			$new['study']->update('study_learn',array(
				'userid'=>$userid,
				'studyid'=>$studyid,
			),array(
				'count_lesson'=>$count_lesson,
				'status'=>1,
				'addtime'=>time(),
			));
		
		}
		
		echo 2;exit;
		
		break;
		

	
}